package com.common.util;

import java.util.UUID;

public class StringUtil {

	/**
	 * 获取uuid
	 * @return
	 */
	public static String getUUID() {
		UUID uuid = UUID.randomUUID();
		String sUuid = uuid.toString();
		String sNewUuid = sUuid.replaceAll("-", "");
		sNewUuid = sNewUuid.toUpperCase();
		return sNewUuid;
	}
	
	/** 
     * 判断字符串是否为空 
     * @param str 
     * null、“ ”、“null”都返回true 
     * @return 
     */  
    public static boolean isNullString(String str) {  
        return (null == str ||  str.length() <= 0 || str.trim()=="" ||str.trim().equals("") || "null".equals(str.trim().toLowerCase()) 
        		||  "undefined".equals(str.trim().toLowerCase()) ) ? true : false;  
    } 
    
	/**
	 * 去除字符串两端的空格，如果为null，则返回空字符串
	 * @param str 要去除空格的字符串
	 * @return 去掉两端空格的字符串
	 */
	public static String trim(String str){
		return (str == null ? "":str.trim()); 
	}
	
	public static boolean isNotBlank(String url) {
		if (url != null && url.length() > 0) {
			return true;
		}
		return false;
	}

	/**
	 * 下划线转驼峰（首字母小写）
	 * @param param
	 * @return
	 */
	public static String underlineToCamel(String param){
		if (param==null||"".equals(param.trim())){
			return "";
		}
		int len=param.length();
		StringBuilder sb=new StringBuilder(len);
		for(int i = 0; i < len; i++) {
			char c=param.charAt(i);
			if (c=='_'){
				if (++i<len){
					sb.append(Character.toUpperCase(param.charAt(i)));
				}
			}else{
				sb.append(c);
			}
		}
		return sb.toString();
	}

	/**
	 * 下划线转Java类名（首字母大写）
	 * @param param
	 * @return
	 */
	public static String underlineToClassName(String param){
		if (param==null||"".equals(param.trim())){
			return "";
		}
		int len=param.length();
		StringBuilder sb=new StringBuilder(len);
		for(int i = 0; i < len; i++) {
			char c=param.charAt(i);
			if (c=='_'){
				if (++i<len){
					sb.append(Character.toUpperCase(param.charAt(i)));
				}
			}else{
				if(i==0){
					sb.append(Character.toUpperCase(c));
				}else{
					sb.append(Character.toLowerCase(c));
				}

			}
		}
		return sb.toString();
	}

	/**
	 * 驼峰转下划线
	 * @param param
	 * @return
	 */
	public static String camelToUnderline(String param){
		if (param==null||"".equals(param.trim())){
			return "";
		}
		int len=param.length();
		StringBuilder sb=new StringBuilder(len);
		for (int i = 0; i < len; i++) {
			char c=param.charAt(i);
			if (Character.isUpperCase(c)){
				sb.append('_');
				sb.append(Character.toLowerCase(c));
			}else{
				sb.append(c);
			}
		}
		return sb.toString();
	}
}
